﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SMS.BaseClass;

namespace SMS.GoodsManage
{
    public partial class frmStockInDataImport : Form
    {
        /// <summary>
        /// 返回值
        /// </summary>
        public List<string> lstReturn = new List<string>();

        SMS.BaseClass.Utils utils = new BaseClass.Utils();

        public frmStockInDataImport()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 导入Ecel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnImport_Click(object sender, EventArgs e)
        {
            try
            {
                ExcelIO excelIO = new ExcelIO();

                if (this.tbxSheetName.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Sheet名不能为空，请重新输入!");
                }

                string strSheetName = this.tbxSheetName.Text.TrimEnd();

                int intExcelVer = this.tbxFilePath.Text.ToLower().EndsWith("xlsx") ? CommonConst.EXCEL_VER_2007 : CommonConst.EXCEL_VER_2003;

                // 将Excel读取为DataTable
                DataTable dtExcel = excelIO.GetDataFromExcel(this.tbxFilePath.Text, strSheetName, intExcelVer);

                this.SetDataGridView(dtExcel);
            }
            catch (Exception)
            {
                MessageBox.Show("读入数据失败，请检查输入的Sheet名与Excel是否一致。");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSFile_Click(object sender, EventArgs e)
        {
            ofDialogFile.InitialDirectory = "C:\\";
            ofDialogFile.Filter = "Excel Files (*.xls;*.xlsx)|*.xls;*.xlsx";
            ofDialogFile.RestoreDirectory = true;
            ofDialogFile.ShowDialog();
            tbxFilePath.Text = ofDialogFile.FileName.ToString().Trim();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbxFilePath_TextChanged(object sender, EventArgs e)
        {
            if (this.tbxFilePath.Text.Trim() != string.Empty)
            {
                this.btnImport.Enabled = true;
                this.tbxSheetName.Text = System.IO.Path.GetFileNameWithoutExtension(this.tbxFilePath.Text.Trim());
            }
            else
            {
                this.btnImport.Enabled = false;
                this.tbxSheetName.Text = string.Empty;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        public void SetDataGridView(DataTable dt)
        {
            if (dt == null)
            {
                MessageBox.Show("Excel 无数据！");
                return;
            }

            this.dgvImportedData.DataSource = dt;

            int count = this.dgvImportedData.Columns.Count;

            for (int i = 0; i < count; i++)
            {
                this.dgvImportedData.Columns[i].SortMode = DataGridViewColumnSortMode.Programmatic;
            }

            this.dgvImportedData.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;

            this.dgvImportedData.MultiSelect = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbxSheetName_TextChanged(object sender, EventArgs e)
        {
            if (this.tbxSheetName.Text.Trim() == string.Empty || this.tbxFilePath.Text.Trim() == string.Empty)
            {
                this.btnImport.Enabled = false;
            }
            else
            {
                this.btnImport.Enabled = true;
            }
        }

        /// <summary>
        /// Grid标题双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvImportedData_ColumnHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strData = string.Empty;

            for (int i = 0; i < this.dgvImportedData.Rows.Count; i++)
            {
                strData = utils.ObjToString(this.dgvImportedData.Rows[i].Cells[e.ColumnIndex].Value).Trim();

                if (this.lstReturn.Contains(strData))
                {
                    continue;
                }
                this.lstReturn.Add(strData);
            }

            this.Close();
        }
    }
}
